package cn.im.qywx.util;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

/**
 * JSON与LIST互转工具类
 * 
 * @author jehone gao
 * 
 *         2017-5-17
 */
public class JsonUtils {
	
	static Gson gson = new  GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
	
	/** 将list转化为json字符串
	 */
	public static String list2json(List<?> list) {
		if(list==null) return "[]";
		String str = gson.toJson(list);
		return str;
	}
	
	/** 将对象转化为json字符串(类 或者 Map)
	 */
	public static String obj2json(Object obj) {
		if(obj==null) return "{}";
		String str = gson.toJson(obj);
		return str;
	}

	/** 将json字符串转化为List<String>
	 */
	public static List<String> json2list(String jsonStr) {
		try{
			List<String> list = gson.fromJson(jsonStr, new TypeToken<List<String>>() {
			}.getType());
			return list;
		}catch(Exception e){
			//e.printStackTrace();
		}
		return null;
	}
	
	/** 将json字符串转化为List<Integer>
	 */
	public static List<Integer> json2listInt(String jsonStr) {
		try{
			List<Integer> list = gson.fromJson(jsonStr, new TypeToken<List<Integer>>() {
			}.getType());
			return list;
		}catch(Exception e){
			//e.printStackTrace();
		}
		return null;
	}
	
	/** 将json字符串转化为List<Map<Object,Object>>
	 */
	public static List<Map<Object,Object>> json2listMap(String jsonStr) {
		try{
			List<Map<Object,Object>> list = gson.fromJson(jsonStr, new TypeToken<List<Map<Object,Object>>>() {
			}.getType());
			return list;
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	/** 将json字符串转化为List<Map<Object,Object>>
	 */
	public static List<Map<String,String>> json2listMapStr(String jsonStr) {
		try{
			List<Map<String,String>> list = gson.fromJson(jsonStr, new TypeToken<List<Map<String,String>>>() {
			}.getType());
			return list;
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	/** 将json字符串转化为List<Object>
	 */
	public static List<Object> json2listObj(String jsonStr) {
		try{
			List<Object> list = gson.fromJson(jsonStr, new TypeToken<List<Object>>() {
			}.getType());
			return list;
		}catch(Exception e){
			//e.printStackTrace();
		}
		return null;
	}
	
	/** 将json字符串转化为List<List<String>
	 */
	public static List<List<String>> json2list2String(String jsonStr) {
		try{
			List<List<String>> list = gson.fromJson(jsonStr, new TypeToken<List<List<String>>>() {
			}.getType());
			return list;
		}catch(Exception e){
			//e.printStackTrace();
		}
		return null;
	}
	
	/** 将json字符串转化为Map<String,Object>
	 */
	public static Map<String,Object> json2Map(String jsonStr) {
		try{
			Map<String,Object> map = gson.fromJson(jsonStr, new TypeToken<Map<String,Object>>() {
			}.getType());
			return map;
		}catch(Exception e){
			//e.printStackTrace();
		}
		return null;
	}
	
	/** 将json字符串转化为Map<String,String>
	 */
	public static Map<String,String> json2MapSingleStr(String jsonStr) {
		try{
			Map<String,String> map = gson.fromJson(jsonStr, new TypeToken<Map<String,String>>() {
			}.getType());
			return map;
		}catch(Exception e){
			//e.printStackTrace();
		}
		return null;
	}
	
	/** 将json字符串转化为Map<String,Map<String,String>>
	 */
	public static Map<String,Map<String,String>> json2MapInMapStr(String jsonStr) {
		try{
			Map<String,Map<String,String>> map = gson.fromJson(jsonStr, new TypeToken<Map<String,Map<String,String>>>() {
			}.getType());
			return map;
		}catch(Exception e){
			//e.printStackTrace();
		}
		return null;
	}
	/** 将json字符串转化为Map<String,List<Integer>
	 */
	public static Map<String,List<Integer>> json2MapInListInt(String jsonStr) {
		try{
			Map<String,List<Integer>> map = gson.fromJson(jsonStr, new TypeToken<Map<String,List<Integer>>>() {
			}.getType());
			return map;
		}catch(Exception e){
			//e.printStackTrace();
		}
		return null;
	}
	
}
